package services;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;
import vos.RoleSearchVo;
import vos.RoleSearchVo;

import com.aton.db.SessionFactory;

import models.Operator;
import models.Role;
import models.Role;
import models.User;
import models.mappers.RoleMapper;
import models.mappers.RoleMapper;
import models.mappers.StandardMapper;
import models.mappers.UserMapper;

public class RoleService {
	/**
	 * 根据id查找角色
	 *
	 * @Title: findByID 
	 * @param roleid
	 * @return
	 * @return: Role
	 */
	public static Role findById(String roleid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
            return mapper.findById(roleid);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据条件统计角色数量
	 *
	 * @Title: countRole 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countRole(RoleSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
            return mapper.countRole(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据条件查询角色列表
	 *
	 * @Title: listRole 
	 * @param vo
	 * @return
	 * @return: List<Role>
	 */
	public static List<Role> listRole(RoleSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
            return mapper.listRole(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @param roleid 
	 * @return: void
	 */
	public static Boolean betchEnable(String ids, String roleid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", roleid);
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
            mapper.betchEnable(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @return: void
	 */
	public static Boolean betchDisable(String ids,String roleid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", roleid);
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
            mapper.betchDisable(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchDel 
	 * @param ids
	 * @return
	 * @return: boolean
	 */
	public static boolean betchDel(String ids) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
            mapper.betchDel(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 删除角色
	 *
	 * @Title: delRole 
	 * @param id 角色id
	 * @return
	 * @return: boolean
	 */
	public static boolean delRole(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
	        mapper.delRole(id);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		
		return true;
	}
	/**
	 * 验证编号是否存在
	 *
	 * @Title: checkCode 
	 * @param roleid 角色编号
	 * @return
	 * @return: boolean
	 */
	public static boolean checkCode(String roleid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
	        String role= mapper.checkCode(roleid);
	        if(StringUtils.isEmpty(role)){
	        	return false;
	        }
	        else{
	        	return true;
	        }
		}catch(Exception e){
			e.printStackTrace();
			return true;
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取角色信息
	 *
	 * @Title: getById 
	 * @param roleid 角色id
	 * @return
	 * @return: Role
	 */
	public static Role getById(String roleid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
			return mapper.getById(roleid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存角色信息
	 *
	 * @Title: saveRole 
	 * @param role 角色对象
	 * @return
	 * @return: boolean
	 */
	public static boolean saveRole(Role role) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
			Date date=new Date();
			role.createtime=date;
	        mapper.saveRole(role);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新角色信息
	 *
	 * @Title: updateRole 
	 * @param role
	 * @return
	 * @return: boolean
	 */
	public static boolean updateRole(Role role) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
			Date date=new Date();
			role.modifytime=date;
	        mapper.updateRole(role);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 统计子表中用户数量
	 *
	 * @Title: countOperator 
	 * @param roleid 角色id
	 * @return
	 * @return: long
	 */
	public static long countOperator(String roleid) {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
	        return mapper.countOperator(roleid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取子表中用户列表
	 *
	 * @Title: getOperator 
	 * @param vo 角色id
	 * @return
	 * @return: List<Operator>
	 */
	public static List<Operator> getOperator(RoleSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			RoleMapper mapper = ss.getMapper(RoleMapper.class);
	        return mapper.getOperator(vo);
		}finally{
			ss.close();
		}
	}
}
